/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package res.elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import plar.core.Element;
import plar.core.Action;
import plar.core.Common;
import plar.core.Parameters;
import plar.core.NoGravity;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.collision.*;

public class Elevator extends Element  {
class AdjustPlayer implements Action {
	boolean fromAtoB=true;
		public AdjustPlayer() {

		}

		public Parameters call(Parameters parameters)

		{

			Element me = (Element) parameters.get("me");
			String subAction = (String) parameters.get("subAction");
			ArrayList<Element> collisions = (ArrayList<Element>) me.getProperty("collisionList");
			
			if (subAction == "main" ) {
				Vec2 pos = me.getPosition();
			//	Common.info(5,"position:"+pos);
				AABB r = (AABB) me.getProperty("AABB");
				Vec2 s = (Vec2) me.getProperty("returnSpeed");
				//Common.info(5,".moving elevator:"+fromAtoB+" "+pos+" "+r.lowerBound);
				boolean before = fromAtoB;
				if(pos.y<=r.lowerBound.y) fromAtoB=true;
				if(pos.y>=r.upperBound.y) fromAtoB=false;
				//if(before!=fromAtoB)
				{
				//Common.info(5,"moving elevator:"+fromAtoB+" "+pos+" "+r.lowerBound);
				Vec2 d;
				if(fromAtoB)
						d = new Vec2(r.upperBound.x-pos.x,r.upperBound.y-pos.y);
				else
					d = new Vec2(r.lowerBound.x-pos.x,r.lowerBound.y-pos.y);

					float m = (float)Math.sqrt((d.x*d.x+d.y*d.y));
					
					d.x=d.x/m;
					d.y=d.y/m;
					
					
					Vec2 b=new Vec2(s.x*d.x,s.y*d.y);
					me.setSpeed(b);
					Common.info(5,"moving elevator:"+b+" "+pos);
				 }
				}
				
			
			return null;
			}
		}
	
	public Elevator() {
	
		
		setProperty("isStatic", true);
		setProperty("isRunnable", true);
		setProperty("friction",10f);
		setProperty("denisity", 5);
		setProperty("AABB", new AABB());
		setProperty("returnSpeed", new Vec2(1f,2f));

		setStaticSprite("res/elements/elevator.png");
		setProperty("canCollide", true);
		
	
		actions.addAction("adjust", new AdjustPlayer());
		actions.addAction("nogravity", new NoGravity());
	}
}
 

